package com.ciwong.epaper.modules.me.ui;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

import com.android.volley.TimeoutError;
import com.aspsine.swipetoloadlayout.OnLoadMoreListener;
import com.aspsine.swipetoloadlayout.OnRefreshListener;
import com.aspsine.swipetoloadlayout.SwipeToLoadLayout;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.epaper.dao.EpaperDao;
import com.ciwong.epaper.modules.epaper.util.EpaperJumpManager;
import com.ciwong.epaper.modules.me.adapter.FavoritesListAdapter;
import com.ciwong.epaper.modules.me.bean.FavoritesBook;
import com.ciwong.epaper.modules.me.bean.FavoritesBookPageBean;
import com.ciwong.epaper.modules.me.widget.MarginDecoration;
import com.ciwong.epaper.util.BaseExtCallBack;
import com.ciwong.epaper.util.IntentFlag;
import com.ciwong.epaper.util.IntentValue;
import com.ciwong.epaper.util.RequestCode;
import com.ciwong.mobilelib.i.BaseQuickOnItemClickListener;
import com.ciwong.mobilelib.ui.BaseActivity;
import com.ciwong.mobilelib.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 收藏书籍页面
 */
public class FavoritesListActivity extends BaseActivity implements OnRefreshListener, OnLoadMoreListener {

    private RecyclerView mRvBookDesk;
    private SwipeToLoadLayout mSwipeToLoadLayout;
    private FavoritesListAdapter favoritesListAdapter;
    private int mBookmarkId;
    private int mPageIndex = 1;
    private int mPageSize = 15;
    private boolean mIsRefesh;
    private LinearLayout list_nodata;
    private ViewGroup listviewgroup;
    private Button btn_retry;
    //书籍列表
    private List<FavoritesBook> favoritesBooks;
    //用于分辨作业从哪个模式进来
    private String tag;


    @Override
    protected int setView() {
        return R.layout.activity_favorites_list;
    }

    @Override
    protected void findViews() {
        mRvBookDesk = findViewById(R.id.swipe_target);
        mSwipeToLoadLayout = findViewById(R.id.swipe_toLoad_layout);
        list_nodata = findViewById(R.id.ll_no_net);
        listviewgroup = findViewById(R.id.listviewgroup);
        btn_retry = findViewById(R.id.btn_retry);
    }

    @Override
    protected void initEvent() {
        mSwipeToLoadLayout.setOnRefreshListener(this);
        mSwipeToLoadLayout.setOnLoadMoreListener(this);
    }

    @Override
    protected void init() {
        setTitleText("布置作业");
        Intent intent = getIntent();
        tag = intent.getStringExtra(IntentFlag.INTENT_FLAG_TAG);
        if (TextUtils.isEmpty(tag)) {
            //如果为空设为默认课后作业
            tag = RequestCode.TeaWorkContent.HOME_WORK;
        }
        mRvBookDesk.setLayoutManager(new GridLayoutManager(FavoritesListActivity.this, 3));
        mRvBookDesk.addItemDecoration(new MarginDecoration(FavoritesListActivity.this));
        favoritesListAdapter = new FavoritesListAdapter(R.layout.activity_favorites_list_item, favoritesBooks);
        mRvBookDesk.setAdapter(favoritesListAdapter);
        favoritesListAdapter.setOnItemClickListener(new BaseQuickOnItemClickListener() {
            @Override
            public void avertRepeatOnClick(BaseQuickAdapter adapter, View view, int position) {
                try {
                    if (favoritesBooks.get(position).getAdd()) {
                        EpaperJumpManager.jumpToSerachBook(R.string.go_back, FavoritesListActivity.this, RequestCode.BookDeskResquestCode.REQ_CODE_TO_SEARCH_BOOK);
                    } else {
                        FavoritesBook favoritesBook = favoritesBooks.get(position);
                        if (null != favoritesBook) {
                            EpaperJumpManager.jumpToCatalog2(R.string.go_back, FavoritesListActivity.this, tag, favoritesBook, IntentValue.JumpToBookCatalogType.FROM_NEW_DESK_BOOK, -1, favoritesBook.getServiceId());
                        }
                    }

                } catch (Exception e) {

                }
            }
        });
    }


    @Override
    protected void loadData() {
        /*setTitleText("收藏列表");
        setTitleBarColor(R.color.favorites_title);*/
        mIsRefesh = true;
        mPageIndex = 1;
        getServiceProductsByTop();
    }

    @Override
    public void onRefresh() {
        mIsRefesh = true;
        mPageIndex = 1;
        getServiceProductsByTop();
    }

    @Override
    public void onLoadMore() {
        mIsRefesh = false;
        mPageIndex++;
        getServiceProductsByTop();
    }

    /**
     * 获取收藏书籍
     */
    public void getServiceProductsByTop() {
        EpaperDao.getInstance().getServiceProductsByTop(EApplication.BRAND_ID, 2
                , mPageIndex, mPageSize, 1, 3, "", new BaseExtCallBack(this, EApplication.getInstance().getUserInfoBase().getUserId() + "") {
                    @Override
                    public void failed(int errorCode, Object data) {
                        hideCricleProgress();
                        mSwipeToLoadLayout.setRefreshing(false);
                        mSwipeToLoadLayout.setLoadingMore(false);
                        super.failed(errorCode, data);
                        failed(data);
                    }

                    @Override
                    public void failed(Object data) {
                        hideCricleProgress();
                        mSwipeToLoadLayout.setRefreshing(false);
                        mSwipeToLoadLayout.setLoadingMore(false);
                        if (data instanceof TimeoutError) {
                            ToastUtil.INSTANCE.toastCenterError(getString(R.string.load_faild_check_net_work));
                        }
                    }

                    @Override
                    public void success(Object data) {
                        hideCricleProgress();
                        mSwipeToLoadLayout.setRefreshing(false);
                        mSwipeToLoadLayout.setLoadingMore(false);
                        if (null != data) {
                            FavoritesBookPageBean favoritesBookPageBean = (FavoritesBookPageBean) data;
                            List<FavoritesBook> favoritesBookList = favoritesBookPageBean.getPageList();
                            if (mIsRefesh) {
                                if (null != favoritesBookList && favoritesBookList.size() > 0) {
                                    favoritesBooks = favoritesBookList;
                                    FavoritesBook favoritesBook = new FavoritesBook();
                                    favoritesBook.setAdd(true);
                                    favoritesBooks.add(0, favoritesBook);
                                    favoritesListAdapter.setNewData(favoritesBooks);
                                } else {
                                    //无数据
                                    if (null == favoritesBooks || favoritesBooks.size() == 0) {
                                        favoritesBooks = new ArrayList<>();
                                        FavoritesBook favoritesBook = new FavoritesBook();
                                        favoritesBook.setAdd(true);
                                        favoritesBooks.add(0, favoritesBook);
                                        favoritesListAdapter.setNewData(favoritesBooks);
                                    }
                                    EpaperJumpManager.jumpToSerachBook(R.string.go_back, FavoritesListActivity.this, RequestCode.BookDeskResquestCode.REQ_CODE_TO_SEARCH_BOOK);
                                }

                            } else {
                                if (null != favoritesBookList && favoritesBookList.size() > 0) {
                                    favoritesBooks.addAll(favoritesBookList);
                                    favoritesListAdapter.setNewData(favoritesBooks);
                                }

                            }
                        }
                    }
                });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == RequestCode.BookDeskResquestCode.REQ_CODE_TO_SEARCH_BOOK && resultCode == Activity.RESULT_OK) { //添加书籍成功
            if (mPageIndex == 1) {
                onRefresh();
            }
        }
    }
}
